package com.lechi.web.service.impl;

import com.lechi.web.common.utils.StringUtils;
import com.lechi.web.framework.log.IotExceptionLog;
import com.lechi.web.job.SendCmdJob;
import com.lechi.web.quartz.scheduler.DynamicJob;
import com.lechi.web.quartz.scheduler.DynamicSchedulerFactory;
import com.lechi.web.service.ISchedulerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * The type Scheduler service.
 *
 * @author YANGGUANG
 */
@Slf4j
@Service("schedulerService")
@RequiredArgsConstructor
public class SchedulerServiceImpl implements ISchedulerService {

    private final DynamicSchedulerFactory dynamicSchedulerFactory;

    /**
     * 添加一个动态的JOB
     */
    @Override
    public void addDynamicJob(String period, List<Map<String, String>> list) {
        String JobName = period + "_Second";
        DynamicJob dynamicJob = createDynamicJob(JobName, period);
        dynamicJob.addJobData("deviceSn:cmd", list);
        if (StringUtils.isEmpty(period) || list.isEmpty()) {
            log.error("SchedulerServiceImpl addDynamicJob period : {} ", period);
            return;
        }
        try {
            boolean ok = dynamicSchedulerFactory.registerJob(dynamicJob, new Date());
            log.info("注册调度任务 [{}] -> {}, 调度参数:{}", dynamicJob, ok, Arrays.deepToString(list.toArray()));
        } catch (Exception e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "注册调度任务", method, dynamicJob));
            log.error("SchedulerServiceImpl addDynamicJob error : {} ", e.getMessage());
        }
    }


    /**
     * 删除一个JOB
     */
    @Override
    public void removeJob(String period) {
        String JobName = period + "_Second";
        final DynamicJob dynamicJob = createDynamicJob(JobName, period);
        try {
            final boolean result = dynamicSchedulerFactory.removeJob(dynamicJob);
            log.info("删除调度任务 [{}] result: {}", dynamicJob, result);
        } catch (SchedulerException e) {
            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            String method = Thread.currentThread().getName() + ":" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName();
            CompletableFuture.runAsync(IotExceptionLog.exceptionLog(e, "删除调度任务", method, dynamicJob));
            log.error("SchedulerServiceImpl removeJob error : {} ", e.getMessage());
        }
    }

    /**
     * 创建 一个动态的JOB
     */
    private DynamicJob createDynamicJob(String jobName, String period) {
        return new DynamicJob(jobName)
                .cronExpression(period)
                .target(SendCmdJob.class);
    }
}